package com.code;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obDoanhSoNV;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TK_DanhSoByNhanVien extends Activity {
	private String strNhomChon= "", strNhomKho = "";
	ArrayList<Integer> date_actual;
	private String date;
	LinearLayout llMain;
	private final int REQUEST_DSNV = 100;
	private Integer choose = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.doanhsobynv);

		strNhomChon = super.getIntent().getExtras().getString("strNhomChon");
		strNhomKho = super.getIntent().getExtras().getString("strKhoChon");
		date_actual = super.getIntent().getExtras()
				.getIntegerArrayList("date_tk_DoanhsoNV");
		llMain = (LinearLayout) findViewById(R.id.llMainDs);

		date = String.valueOf(date_actual.get(2))
				+ DetailCuocGoi.setMonth(date_actual.get(1))
				+ Global.formatDay(date_actual.get(0));
		choose = super.getIntent().getExtras().getInt("chose_DSNV");
		StringBuffer sql = new StringBuffer(
				" SELECT a.DDBH as MANV,empl.EmployeeName as Name,a.SL as SL,a.DoanhSo as DoanhSo FROM(");
		sql.append(" SELECT px.DDBH,SUM(vattuxuat.SL)as SL,CASE WHEN ISNULL(SUM((vattuxuat.SL * vattuxuat.dongia) * ");
		sql.append(" (1 + ISNULL(vattuxuat.Thue, 0))),'') = '' THEN 0 ");
		sql.append(" ELSE SUM((vattuxuat.SL * vattuxuat.dongia) * (1 + ISNULL(vattuxuat.Thue, 0))) ");
		sql.append(" END AS DoanhSo ");
		sql.append(" FROM Phieuxuat px INNER JOIN vattuxuat ON px.SoCt = vattuxuat.SoCT ");
		sql.append(" INNER JOIN Products ON vattuxuat.[Product ID] = Products.[Product ID] ");
		sql.append(" INNER JOIN EMPLOYEE empl ON empl.EmployeeID = px.DDBH ");
//		if (choose == TK_ChonMau.CHOOSE_NAM)
		sql.append(" AND DATEPART(YYYY,px.Ngay) = DATEPART(YYYY,'"	+ date + "')");
	if (choose == TK_ChonMau.CHOOSE_QUI)
			sql.append(" AND DATEPART(quarter,px.Ngay) = DATEPART(quarter,'"
					+ date + "')");
		else if (choose == TK_ChonMau.CHOOSE_THANG)
			sql.append(" AND DATEPART(MM,px.Ngay) = DATEPART(MM,'"
					+ date + "')");
		else if (choose == TK_ChonMau.CHOOSE_TUAN)
			sql.append(" AND DATEPART(WEEK,px.Ngay) = DATEPART(WEEK,'"
					+ date + "')");
		else if(choose == TK_ChonMau.CHOOSE_NGAY)
			sql.append(" AND CAST(px.Ngay as date) = CAST('"
					+ date + "' as date)");
		if(!strNhomKho.equals(""))
		{
		sql.append(" AND px.MSK COLLATE DATABASE_DEFAULT ");
		sql.append(" IN (SELECT value FROM  dbo.fn_Split('" + strNhomKho
				+ "', ','))");
		}
		if (!strNhomChon.equals(""))
			sql.append(" AND Products.[Category ID] ='" + strNhomChon+"'");
		sql.append(" GROUP BY px.DDBH) a ");
		sql.append("INNER JOIN EMPLOYEE empl ON empl.EmployeeID = a.DDBH");
		sql.append( " ORDER BY a.DoanhSo DESC ");
		loadData(sql.toString());
	}

	public void loadData(String sql) {
		Float total = 0.0f;
		Integer totalSL = 0;
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		final TableLayout tL = new TableLayout(this);
		llMain.addView(tL);
		TableRow tR = new TableRow(this);
		tR.setWeightSum(4.0f);
		// column nhan vien
		TextView tvNhanVien = new TextView(this);
		tvNhanVien.setText("Nhân viên");
		TK_TongMatHang.setLayoutForTextView(tvNhanVien, true, Gravity.CENTER,
				10, 2.0f);
		// column so luong
		TextView tvSL = new TextView(this);
		tvSL.setText("SL");
		TK_TongMatHang.setLayoutForTextView(tvSL, true, Gravity.CENTER, 10,
				1.0f);
		// column thanh tien
		TextView tvThanhtien = new TextView(this);
		tvThanhtien.setText("Thành tiền");
		TK_TongMatHang.setLayoutForTextView(tvThanhtien, true, Gravity.RIGHT,
				10, 1.0f,true);
		tR.addView(tvNhanVien);
		tR.addView(tvSL);
		tR.addView(tvThanhtien);
		tL.addView(tR);
		try {
			while (rs.next()) {
				final obDoanhSoNV odsNV = new obDoanhSoNV();
				if (rs.getString("Name") != null) {
					odsNV.setTenNV(rs.getString("Name"));
				}
				if (rs.getString("SL") != null) {
					odsNV.setSL(String.valueOf(rs.getInt("SL")));
					totalSL = totalSL + rs.getInt("SL");
				}
				if (rs.getString("DoanhSo") != null) {
					odsNV.setThanhTien(rs.getString("DoanhSo"));
					total = total + Float.valueOf(rs.getString("DoanhSo"));
				}
				if(rs.getString("MaNV") != null)
				{
					odsNV.setMaNV(rs.getString("MaNV"));					
				}
				TableRow tRow = new TableRow(this);
				tRow.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						for (int i = 0; i < tL.getChildCount(); i++) {
							tL.getChildAt(i).setBackgroundColor(
									Color.TRANSPARENT);
						}
						v.setBackgroundColor(Color.YELLOW);
					}
				});
				tRow.setOnLongClickListener(new OnLongClickListener(){					
					public boolean onLongClick(View v) {
						// TODO Auto-generated method stub
						
						if(!odsNV.getMaNV().equals("") &&( choose == TK_ChonMau.CHOOSE_NGAY))
						{
							Intent i = new Intent(TK_DanhSoByNhanVien.this,DetailDoanhSoNV.class);
							Bundle b = new Bundle();
							b.putString("strNhom", strNhomChon);
							b.putString("strKho", strNhomKho);
							b.putString("manhanvien", odsNV.getMaNV());
							b.putIntegerArrayList("date_actual_ds", date_actual);
							b.putInt("choose_dsnv", choose);
							i.putExtras(b);
							startActivityForResult(i, REQUEST_DSNV);
						}
						return false;
					}
				});
				tRow.setWeightSum(4.0f);				
				// column nhan vien
				TextView tvNhanViens = new TextView(this);
				tvNhanViens.setText(odsNV.getTenNV());
				TK_TongMatHang.setLayoutForTextView(tvNhanViens,false,
						Gravity.CENTER, 10, 2.0f);
				// column so luong
				TextView tvSLs = new TextView(this);
				tvSLs.setText(odsNV.getSL());
				TK_TongMatHang.setLayoutForTextView(tvSLs, false,
						Gravity.CENTER, 10, 1.0f);
				// column thanh tien
				TextView tvThanhtiens = new TextView(this);
				tvThanhtiens.setText(Global.formatDecimal(Float.valueOf(odsNV.getThanhTien())));
				TK_TongMatHang.setLayoutForTextView(tvThanhtiens, false,
						Gravity.RIGHT, 10, 1.0f,true);
				tRow.addView(tvNhanViens);
				tRow.addView(tvSLs);
				tRow.addView(tvThanhtiens);
				tL.addView(tRow);
			}
			// Rows End 
			TableRow tREnd = new TableRow(this);
			tREnd.setWeightSum(4.0f);

			TextView tvEndNV = new TextView(this);
			tvEndNV.setText("Tổng");
			TK_TongMatHang.setLayoutForTextView(tvEndNV, false,
					Gravity.CENTER, 10, 2.0f);
			tvEndNV.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvEndSL = new TextView(this);
			tvEndSL.setText(String.valueOf(totalSL));
			TK_TongMatHang.setLayoutForTextView(tvEndSL, false,
					Gravity.CENTER, 10, 1.0f);
			tvEndSL.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvEndThanhTien = new TextView(this);
			tvEndThanhTien.setText(Global.formatDecimal(total).toString());
			TK_TongMatHang.setLayoutForTextView(tvEndThanhTien, false,
					Gravity.CENTER, 10, 1.0f,true);
			tvEndThanhTien.setBackgroundResource(R.layout.border);
			tvEndThanhTien.setGravity(Gravity.RIGHT);

			tREnd.addView(tvEndNV);
			tREnd.addView(tvEndSL);
			tREnd.addView(tvEndThanhTien);
			tL.addView(tREnd);
			
			rs.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}
}
